package rabbitMQConnection;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.apache.commons.pool2.ObjectPool;
import org.springframework.beans.factory.annotation.Value;

import java.awt.print.Book;

/**
 * RabbitMQConnectionPool
 * 连接池
 * 提高性能不用每次发送消息时都连接
 *
 * @Author:Cjj
 * @Date:2025/5/10
 */
public class RabbitMQConnectionPool {
    private static ObjectPool pool;

    @Value(value = "${rabbitmq.host:localhost}")
    private String host;
    @Value(value = "${rabbitmq.port:5672}")
    private int port;
    @Value(value = "${rabbitmq.username:admin}")
    private String username;
    @Value(value = "${rabbitmq.password:admin}")
    private String password;

    public RabbitMQConnectionPool(String host, int port, String username, String password) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public Connection getConnection() {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(host);
            factory.setPort(port);
            factory.setUsername(username);
            factory.setPassword(password);
            factory.setConnectionTimeout(3000);//设置连接超时时间
            factory.setRequestedHeartbeat(60);//设置心跳
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }


    public void returnConnection(Connection connection) {
        if (connection != null) {
            try {
                pool.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    


   /*  public Connection getConnection(){
        try{
            return pool.borrowObject();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //返回连接到连接池
    public void returnConnection(Connection connection){
        if(connection!=null){
            try{
                pool.returnObject(connection);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

   @PostConstruct
    private void initializePool(){
        try{
           ConnectionFactory factory = new ConnectionFactory();
           factory.setHost(host);
           factory.setPort(port);
           factory.setUsername(username);
           factory.setPassword(password);
           factory.setConnectionTimeout(3000);//设置连接超时时间
           factory.setRequestedHeartbeat(60);//设置心跳

            GenericObjectPoolConfig<Connection> config= new GenericObjectPoolConfig();
            config.setMaxTotal(10);//设置最大连接数
            config.setMaxIdle(5);//设置最小空闲连接数
            config.setBlockWhenExhausted(true);//允许在连接池耗尽时等待
            config.setMaxWaitMillis(1000);//设置最大等待时间

          pool= new GenericObjectPool<>(new BasicConnectionFactory<Connection>(){
                @Override
                public Connection makeObject() throws Exception {
                    return factory.newConnection();
                }

               @Override
               public void destroyObject(PooledObject<Connection> pooledObject)throws Exception{
                   Connection conn = pooledObject.getObject();
                   if(conn!=null){
                       conn.close();
                   }

               }

               @Override
               public boolean validateObject(PooledObject<Connection> pooledObject){
                    Connection conn = pooledObject.getObject();
                    return conn!=null && conn.isOpen();
               }
               @Override
               public PooledObject<Connection> wrap(Connection conn){6
                   return new DefaultPooledObject<>(conn);
               }
           },config);
        }catch (Exception e){
            e.printStackTrace();
        }

   }*/

    public static void main(String[] args) {
        System.out.println("aaa");
    }

}
